home *** CD-ROM | disk | FTP | other *** search
- #include <assert.h>
- #include <string.h>
- #include <stdio.h>
-
- #if PRAGMA_ALIGN_SUPPORTED
- #pragma options align=mac68k
- #endif
-
- typedef struct CIconHeader
- {
- PixMap iconPMap;
- BitMap iconMask;
- BitMap iconBMap;
- Handle iconData;
- } CIconHeader;
-
- typedef struct ColorTable256
- {
- long ctSeed;
- short ctFlags;
- short ctSize;
- ColorSpec ctTable[256];
- } ColorTable256;
-
- typedef struct CIconResource
- {
- CIconHeader cicn;
- UInt32 theMask[32];
- UInt32 theBitMap[32];
- ColorTable256 theCTable;
- UInt8 thePixels[32 * 32];
- } CIconResource;
-
- #if PRAGMA_ALIGN_SUPPORTED
- #pragma options align=reset
- #endif
-
- void GetVersion(char* outString);
- void GenerateHeader(char* inName, char* inVersion);
- void GenerateRez(CIconResource* inCicn);
- CIconResource* MakeCICN(FSSpecPtr inSpec);
-
- CIconResource* CreateCICN(void);
- void FillCICNClut(CIconResource* ioCicn);
- OSType GetFileType(FSSpecPtr inSpec);
- short FindIconID(OSType inFileType);
- void FillCICNBitMap(CIconResource* ioCICN, short inResID);
- void FillCICNPixMap(CIconResource* ioCICN, short inResID);
-
- int main(void)
- {
- short resRef, oldResRef;
- CIconResource* cicn;
- char name[256];
- char version[256];
- FSSpec spec;
-
- oldResRef = CurResFile();
-
- FSMakeFSSpec(0, 0, "\pRestricted:Numbers:BSNG 2.5:BSNG 2.5 (PPC)", &spec);
-
- // Get filename.
- BlockMoveData(spec.name + 1, name, spec.name[0]);
- name[spec.name[0]] = '\0';
-
- resRef = FSpOpenResFile(&spec, fsRdPerm);
- assert(resRef);
-
- UseResFile(resRef);
-
- // Get version data.
- GetVersion(version);
-
- // Get icon data.
- cicn = MakeCICN(&spec);
-
- UseResFile(oldResRef);
- CloseResFile(resRef);
-
- // Now spit out data.
- GenerateHeader(name, version);
- GenerateRez(cicn);
-
- DisposePtr((Ptr) cicn);
-
- return 0;
- }
-
-
-
- void GenerateHeader(char* inName, char* inVersion)
- {
- char outString[32];
- FILE* fp;
-
- assert(inName && inVersion);
-
- // If the name + version is too long,
- if (strlen(inName) + strlen(inVersion + 1) > 27)
- {
- // And the name by itself is too long,
- if (strlen(inName) > 27)
- {
- // Truncate the name and trail it with ...
- strncpy(outString, inName, 27);
- outString[26] = '…';
- outString[27] = '\0';
- }
- else
- {
- // Use only the name if it fits.
- strcpy(outString, inName);
- }
- }
- else
- {
- // Use name + version if it fits.
- strcpy(outString, inName);
- strcat(outString, " ");
- strcat(outString, inVersion);
- }
-
- // Create the .h file.
- fp = fopen("BSNG Product.h", "w");
-
- fprintf(fp, "/* BSNG Product.h */\r");
- fprintf(fp, "/* This file is generated by BSNG Product DeRez once per product. */\r");
- fprintf(fp, "/* Feel free to alter the product description, up to 27 chars. max */\r\r");
- fprintf(fp, "#define PROGRAM_NAME \"%s\"\r\r", outString);
-
- fclose(fp);
- }
-
-
- void GenerateRez(CIconResource* inCicn)
- {
- FILE* fp;
- long i, length;
- UInt8* cicnP;
-
- assert(inCicn);
-
- fp = fopen("BSNG Product.r", "w");
-
- fprintf(fp, "/* BSNG Product.r */\r");
- fprintf(fp, "/* This file is generated by BSNG Product DeRez once per product. */\r\r");
- fprintf(fp, "#include \"BSNG Product.h\"\r\r");
- fprintf(fp, "data 'cicn' (1000) {\r");
-
- length = sizeof(CIconResource);
- cicnP = (UInt8*) inCicn;
- for (i = 0; i < length; i++)
- {
- if ((i % 16) == 0)
- {
- fprintf(fp, " $\"");
- }
- else if ((i % 2) == 0)
- {
- fprintf(fp, " ");
- }
-
- fprintf(fp, "%02X", (int) cicnP[i]);
-
- if ((i % 16) == 15)
- {
- fprintf(fp, "\"\r");
- }
- }
-
- if ((i % 16) != 0)
- {
- fprintf(fp, "\"\r");
- }
-
- fprintf(fp, "};\r\r");
-
- fclose(fp);
- }
-
-
-
- // spit out BSNG Product.r
-
- // get file name
- // get 'vers' info
- // spit out BSNG Product.h
-
- void GetVersion(char* outString)
- {
- Handle versH;
- StringPtr str;
-
- versH = Get1Resource('vers', 2);
- if (versH == NULL)
- versH = Get1Resource('vers', 1);
- assert(versH);
-
- HLock(versH);
-
- str = (UInt8*) (*versH + 6);
- BlockMoveData(str + 1, outString, str[0]);
- outString[str[0]] = '\0';
-
- HUnlock(versH);
- ReleaseResource(versH);
- }
-
-
-
- CIconResource* MakeCICN(FSSpecPtr inSpec)
- {
- CIconResource* cicn;
- short id;
-
- cicn = CreateCICN();
- assert(cicn);
-
- FillCICNClut(cicn);
-
- id = FindIconID(GetFileType(inSpec));
- FillCICNPixMap(cicn, id);
- FillCICNBitMap(cicn, id);
-
- return cicn;
- }
-
-
-
- OSType GetFileType(FSSpecPtr inSpec)
- {
- FInfo finderInfo;
-
- FSpGetFInfo(inSpec, &finderInfo);
- return finderInfo.fdType;
- }
-
-
- short FindIconID(OSType inFileType)
- {
- Str255 name;
- OSType type;
- short outID = -1;
- long i, count;
- Handle frefH;
-
- // Iterate FREF resources until we find one whose type (1st 4 bytes) == the file type.
- count = Count1Resources('FREF');
- for (i = 1; i <= count; i++)
- {
- frefH = Get1IndResource('FREF', i);
- assert(frefH);
- if (**(OSType**) frefH == inFileType)
- {
- GetResInfo(frefH, &outID, &type, name);
- }
- ReleaseResource(frefH);
- }
-
- return outID;
- }
-
- void FillCICNClut(CIconResource* ioCicn)
- {
- CTabHandle clutH;
- ColorTable* clutP;
- long i;
-
- assert(ioCicn);
-
- // CLUT 8 is the standard 8-bit color system color table and is always present
- clutH = GetCTable(8);
- assert(clutH);
-
- // Copy the data into the cicn's color table.
- HLock((Handle) clutH);
- clutP = *clutH;
-
- ioCicn->theCTable.ctSeed = 0;
- ioCicn->theCTable.ctFlags = 0;
- ioCicn->theCTable.ctSize = clutP->ctSize;
- for (i = 0; i <= clutP->ctSize; i++)
- {
- ioCicn->theCTable.ctTable[i].value = i;
- ioCicn->theCTable.ctTable[i].rgb = clutP->ctTable[i].rgb;
- }
-
- HUnlock((Handle) clutH);
- }
-
-
- void FillCICNPixMap(CIconResource* ioCICN, short inResID)
- {
- UInt8* icl8P;
- Handle icl8H;
- long i;
-
- assert(ioCICN);
-
- // Load the 'icl8' resource specified by inResID and paste it into this cicn's
- // pixmap data.
-
- icl8H = Get1Resource('icl8', inResID);
- assert(icl8H);
- assert(GetHandleSize(icl8H) == 1024);
-
- HLock(icl8H);
- icl8P = (UInt8*) *icl8H;
- for (i = 0; i < 1024; i++)
- {
- ioCICN->thePixels[i] = icl8P[i];
- }
-
- HUnlock(icl8H);
- ReleaseResource(icl8H);
- }
-
-
-
- void FillCICNBitMap(CIconResource* ioCICN, short inResID)
- {
- UInt32* iconP;
- Handle iconH;
- long i;
-
- assert(ioCICN);
-
- // Load the ICN# resource specified by inResID and paste it into this cicn's
- // pixmap data.
-
- iconH = Get1Resource('ICN#', inResID);
- assert(iconH);
- assert(GetHandleSize(iconH) == 256);
-
- HLock(iconH);
-
- iconP = (UInt32*) *iconH;
- for (i = 0; i < 32; i++)
- {
- ioCICN->theBitMap[i] = iconP[i];
- }
-
- iconP += 32;
- for (i = 0; i < 32; i++)
- {
- ioCICN->theMask[i] = iconP[i];
- }
-
- HUnlock(iconH);
- ReleaseResource(iconH);
- }
-
-
-
- CIconResource* CreateCICN(void)
- {
- Rect theRect;
- int theImageRowBytes, theBitsPixel;
- CIconResource* outCICN;
- PixMap* pm;
- BitMap* bm;
-
- SetRect(&theRect, 0, 0, 32, 32);
-
- // Use an 8-bit pixel map.
- theBitsPixel = 8;
- theImageRowBytes = 0x8020;
-
- outCICN = (CIconResource*) NewPtr(sizeof(CIconResource));
- assert(outCICN);
-
- // Initialize the headers.
- pm = &outCICN->cicn.iconPMap;
- pm->baseAddr = 0;
- pm->rowBytes = theImageRowBytes;
- pm->bounds = theRect;
- pm->pmVersion = 0;
- pm->packType = 0;
- pm->packSize = 0;
- pm->hRes = 0x00480000; // 72 dpi in Fixed format
- pm->vRes = 0x00480000;
- pm->pixelType = 0;
- pm->pixelSize = theBitsPixel;
- pm->cmpCount = 1;
- pm->cmpSize = theBitsPixel;
- pm->planeBytes = 0;
- pm->pmTable = 0;
- pm->pmReserved = 0;
-
- bm = &outCICN->cicn.iconMask;
- bm->baseAddr = 0;
- bm->rowBytes = 4;
- bm->bounds = theRect;
-
- bm = &outCICN->cicn.iconBMap;
- bm->baseAddr = 0;
- bm->rowBytes = 4;
- bm->bounds = theRect;
-
- outCICN->cicn.iconData = 0;
-
- return outCICN;
- }
-